﻿using Controller.i18n.entity;
using HBase;
using hCommand;
using hconnect;
using System;
using System.Collections.Generic;
using System.Threading;

namespace Controller
{
    public abstract class CraneConnect : AbsTaskConnect
    {
        public override String getCPU_SlotNO()
        {
            return "01";
        }

        //复位
        public virtual bool Reset()
        {
            bool result = writeShort(500, 26, 2);
            if (result == true)
            {
                fileAll(InfoC.fileAll_crane_emergencyStop, "堆垛机复位发送成功");
            }
            else if (result == false)
            {
                fileAll(InfoC.fileAll_crane_emergencyStop, "堆垛机复位发送失败");
            }
            return result;
        
        }

        //急停
        public virtual bool EmergencyStop()
        {
            bool result = writeShort(500, 26, 1);

            if (result == true)
            {
                fileAll(InfoC.fileAll_crane_emergencyStop, "堆垛机急停发送成功");
            }
            else if (result == false)
            {
                fileAll(InfoC.fileAll_crane_emergencyStop, "堆垛机急停发送失败");
            }
            return result;
        }
        //解警


        public byte[] all = new byte[8];
        public void set4(int index, int value)
        {

            S7ByteUtil.margnS7L4(all, index, value);
        }

        public void set2(int index, int value)
        {
            S7ByteUtil.margnS7L2(all, index, value);
        }

        protected override bool send(object t, int begin)
        {
            TaskCrane task = (TaskCrane)t;

            List<byte> list = new List<byte>();
            int type = 0;
            type = 5;
            list.AddRange(S7ByteUtil.toS7L2(type));
            int src =0;
            int des = 0;
            if (task.getSrc() != 0 && task.getSrc() < 1000)
            {
                list.AddRange(S7ByteUtil.toS7L2(0));
                list.AddRange(S7ByteUtil.toS7L2(0));
                list.AddRange(S7ByteUtil.toS7L2(0));
                src = task.getSrc().GetXiangDaoStnNo();
            }
            else
            {
                list.AddRange(S7ByteUtil.toS7L2((ushort)task.sz));
                list.AddRange(S7ByteUtil.toS7L2((ushort)task.sx));
                list.AddRange(S7ByteUtil.toS7L2((ushort)task.sy));

                src = 0;
            }
            //取料站台
            list.AddRange(S7ByteUtil.toS7L2(src));
            
            if (task.getDes() != 0 && task.getDes() < 1000)
            {
                list.AddRange(S7ByteUtil.toS7L2(0));
                list.AddRange(S7ByteUtil.toS7L2(0));
                list.AddRange(S7ByteUtil.toS7L2(0));
                des = task.getDes();
            }
            else
            {
                list.AddRange(S7ByteUtil.toS7L2((ushort)task.dz));
                list.AddRange(S7ByteUtil.toS7L2((ushort)task.dx));
                list.AddRange(S7ByteUtil.toS7L2((ushort)task.dy));
                des = 0;
            }
            //取放巷道号
            list.InsertRange(10,S7ByteUtil.toS7L2((ushort)AnalysisXiangDaoNo(task.sz, task.dz, src, des,task.taskNo)));
            //放料站台
            list.AddRange(S7ByteUtil.toS7L2(des.GetXiangDaoStnNo()));
            list.AddRange(S7ByteUtil.toS7L4(task.taskNo));
            writeByteArr(500, 0, list.ToArray());
            writeShort(500, 24, 1);
            return true;
        }

        ushort ReturnInOutZ(int z, int stn)
        {
            if (z <= 4 && stn / 10 == 4)
            {
                return 1;
            }
            if (z > 4 && z <= 8 && stn / 10 == 3)
            {
                return 2;
            }
            if (z > 8 && z <= 12 && stn / 10 == 2)
            {
                return 3;
            }
            if (z > 12 && z <= 16 && stn / 10 == 1)
            {
                return 4;
            }
            return 0;
        }
        ushort ReturnKuNeiZ(int sz, int dz)
        {
            if (sz <= 4 && dz <= 4)
            {
                return 1;
            }
            if (sz > 4 && sz <= 8 && dz > 4 && dz <= 8)
            {
                return 2;
            }
            if (sz > 8 && sz <= 12 && dz > 8 && dz <= 12)
            {
                return 3;
            }
            if (sz > 12 && sz <= 16 && dz > 12 && dz <= 16)
            {
                return 4;
            }
            return 0;
        }
        int AnalysisXiangDaoNo(int sz, int dz, int sStn, int dStn,int taskNo)
        {
            //出库
            if (sz != 0 && dStn != 0)
            {
                return ReturnInOutZ(sz, dStn);
            }
            //入库
            if (sStn != 0 && dz != 0)
            {
                return ReturnInOutZ(dz, sStn);
            }
            //库内
            if (sz != 0 && dz != 0)
            {
                return ReturnKuNeiZ(sz, dz);
            }
            return 0;
        }

        public void reset()
        {
        }
    }
}
